APM.ConfigModulel7Controller = Ember.Controller.extend({
	init : function() {
		this._super();
		this.initConfData();
	},
	
	form: {
		moduleName:"",
		L7:"",
		soFile:"",
		moduleId:"",
		confType:"7",
		errorMessage:"",
		protocol:"",
		code:"",
		l7_proto:"",
		mstatus: 0 ,
		id:""
	},  
	
	initConfData: function(){
   		var ctrl = this;
   		ctrl.store.ajax("GET", "configs/modulel7").then(function(response){
   	 		var modules=response.modules;
   	 		var forEach = Ember.EnumerableUtils.forEach;
   	 		if(modules){
				forEach(modules, function(module){
						module.time = APM.util.formatDate(module.updateTime, "yyyy-MM-dd H:mm");
						if(module.id == "1"){
							module.show = false;
						}else{
							module.show = true;
						}
				});
   				ctrl.set("modules",Ember.A(modules));
   	 		}
   		});
   	},
	 
	//模块查询列表存放数组
	modules : Ember.A(),

	//模块返回码查询列表存放数组
	codeArrys : Ember.A(), 

	modCodeStatus : Ember.A([ {
		"name" : "正确",
		"code" : 0
	} ,
	{
		"name" : "错误",
		"code" : 1
	} ,
	{
		"name" : "忽略",
		"code" : 2
	}]),

	/*
		列配置
	*/
	gridColumns: Ember.computed(function(){
		var columns = [
			{
				headerText: "返回码",
				dataKey: "code",
				allowSort: false,
				width: 120
			},
			{
				headerText: "状态",
				dataKey:"statusName",
				width: 120,
				allowSort: false,
				cellFormatter: APM.util.indicatorCellFormatter
			},
			{
				headerText: "返回码信息",
				width: 150,
				ensurePxWidth:true,
				allowSort: false,
				dataKey:"errorMessage",
				cellFormatter: function (args, colume) {
					if (args.row.type & $.wijmo.wijgrid.rowType.data) {
						var data = args.row.data;
						var value = data.errorMessage;
						var $item = $('<div class="column-summary" style="text-align:center;"></div>').appendTo(args.$container);
						$item.html(APM.util.escape(value));
						args.$container.attr("title",value);
						return true;
					}
				}
			}
		];
		if(!this.get("auth.isNormal")){
			columns.push({
				headerText: "操作",
				dataKey:"id",
				width: 200,
				allowSort: false,
				ensurePxWidth: true,
				cellFormatter: function (args, colume) {
					if (args.row.type & $.wijmo.wijgrid.rowType.data) {
						var ctrl = this.get("controller"),
						data = args.row.data,
						$btnGroup = $('<div class="setcode-edit"></div>').appendTo(args.$container);
						var ub = $('<button class="u-btn-blue" type="button" title="编辑">编辑</button>');
						ub.appendTo($btnGroup).on("click",function(){
							ctrl.send('codeOpenDialog',data);						
						});
						var cb = $('<button class="u-btn-danger ml5" type="button" title="删除">删除</button>');
						cb.appendTo($btnGroup).on("click",function(){
							ctrl.send('delModeCode',data);
						});
						return true;
					}
				}
			});
		}
		return columns;
	}),

	checkFormValid: function() {
	 	var ctrl=this;
		var form = ctrl.get("form");
		
		ctrl.set("formTip", null);
		ctrl.set("saveResult", null);
		
		if(form.code){
			var reg = new RegExp("^[0-9]*$"); 
			if(!reg.test(form.code)){  
			   ctrl.set("formTip", "返回码为数字!");
			   return false;
			}
		}else{
			 ctrl.set("formTip", "返回码不能为空!");
			 return false;
		}
		return true;
	},
	
	actions: {
		gridSorted:function(e, data, view){
			view.ensureControl(false);
		},

		reset : function() {
			this.set("formTip", null);
			this.set("saveResult", null);
		},

		//查询所有模块列表
		queryModules:function(){
			var ctrl = this;
	   		ctrl.store.ajax("GET", "configs/modulel7").then(function(response){
	   	 		var modules=response.modules;
	   	 		var forEach = Ember.EnumerableUtils.forEach;
	   	 		if(modules){
	   	 			forEach(modules, function(module){
						module.time = APM.util.formatDate(module.updateTime, "yyyy-MM-dd H:mm");
						if(module.id == "1"){
							module.show = false;
						}else{
							module.show = true;
						}
					});
	   				ctrl.set("modules",Ember.A(modules));
	   	 		}
	   		});
		},
		
		//对话框操作方法
		 openDialog: function(dioIndex){
		 	this.set("formTip", null);
		 	this.set("saveResult", null);
		 	this.set("form.mstatus", null);
			 if(dioIndex=="1"){
				var dialog = Broada.getView("modulel7_add1");
				Broada.getView("modulel7_add1").openDialog();
			 }
			 if(dioIndex=="2"){
		         var dialog = Broada.getView("modulel7_add2");
		         Broada.getView("modulel7_add2").openDialog();
			 }
			 if(dioIndex=="5"){
			 	 this.set("form.code",null);
		 	 	 this.set("form.errorMessage",null);
		         var dialog = Broada.getView("modulel7_add5");
		         Broada.getView("modulel7_add5").openDialog();
			 }
		 },

		 setOpenDialog: function(l7_proto){
		 	var ctrl=this;
		 	 ctrl.set("formTip", null);
		 	 ctrl.set("saveResult", null);
		 	 ctrl.set("form.l7_proto",l7_proto);
		 	 ctrl.store.ajax("GET", "configs/modulel7/recodes", {
				l7_proto : l7_proto
			}).then(function(response) {
				if(response.status=="success"){
					//todo: 保存成功逻辑
					//alert("codeArrys:"+response.codeArr);
					if(response.codeArr){
							var forEach = Ember.EnumerableUtils.forEach;
							var codeArrays=Ember.A(response.codeArr);
							forEach(codeArrays, function(errorCode){
									var s=errorCode.status;
									if(s==0)
										errorCode['statusName']='正确';
									if(s==1)
										errorCode['statusName']='错误';
									if(s==2)
										errorCode['statusName']='忽略';
							});
						ctrl.set("codeArrys", codeArrays);
					}
				}
			});
		     //var dialog = Broada.getView("modulel7_add3");
		     //Broada.getView("modulel7_add3").openDialog();
		     //$("#modulel7_add3").parent().append($("#modulel7_add3_save"));

		     // 弹窗从侧边滑出样式
		    var view = Broada.getView("modulel7_add3");
		    var o = view.$();

		    // 获取弹框出现位置
		    var $modulel7Tab = $("table.conf-modulel7-tab");
		    var top = $modulel7Tab.offset().top + $(".application-main").scrollTop();
		    var left = $modulel7Tab.offset().left + $modulel7Tab.width() - o.width() - 60;
			ctrl.set("view",o);
			o.parent().removeClass("bounceOutRight animated");
			o.parent().removeClass("bounceInRight animated");
			view.openDialog();
			o.parent().css({"left":left+"px"});
			o.parent().css({"top":top+"px"});
			// 按钮位置更改
			$("#modulel7_add3").parent().find(".ui-dialog-titlebar").after($(".setcode-options"));

			o.parent().addClass("bounceInRight animated");	

			o.parent().on("click",function(event){
				if($(event.target).hasClass("u-btn-blue")){
					return;
				}
				event.stopPropagation();
			});

			var time = setTimeout(function(){
			 	ctrl.set("boolean",true);
			 },1000);

			$(document).on("click",function(e){
				 // 出现模态框不执行
				 if($(".ui-dialog-overlay").length){
				 	return;
				 }
				 // 模态框消失不触发事件
				 if($(e.target).hasClass("btn") || $(e.target).hasClass("u-btn-blue") || 
				 	$(e.target).parents(".broada-ui-messages").length ||
				 	$(e.target).parents(".broada-ui-message").length){
				 	return;
				 }
				 // if(!ctrl.get("boolean")){
				 // 	return;
				 // };
				 o.parent().removeClass("bounceInRight animated");
				 o.parent().addClass("bounceOutRight animated");
				 ctrl.set("boolean",false);
			});
		 },

		 codeOpenDialog: function(obj){
		 	 this.set("saveResult", null);
		 	 this.set("formTip", null);
		 	 this.set("form.id",obj.id);
		 	 this.set("form.code",obj.code);
		 	 this.set("form.mstatus",obj.status);
		 	 this.set("form.protocol",obj.protocol);
		 	 this.set("form.errorMessage",obj.errorMessage);
		 	 $("#modulel7_add4 .select-menu-wrapper .select-content").text(obj.statusName);
		     var dialog = Broada.getView("modulel7_add4");
		     Broada.getView("modulel7_add4").openDialog();

		     // 调整保存按钮样式问题
		     //$(".modulel7-editcode").after($(".editcode-content").parent());
		 },
		 
		 closeDialog: function(dioIndex){
		 	this.set("saveResult", null);
		 	this.set("formTip", null);
			 if(dioIndex=="1"){
				Broada.getView("modulel7_add1").closeDialog();
			 }
			 if(dioIndex=="2"){
					Broada.getView("modulel7_add2").closeDialog();
			}
			if(dioIndex=="3"){
				Broada.getView("modulel7_add3").closeDialog();
			}
			if(dioIndex=="4"){
				Broada.getView("modulel7_add4").closeDialog();
			}
			if(dioIndex=="5"){
				Broada.getView("modulel7_add5").closeDialog();
			}
		 },
		 
		//添加模块方法,目前改为导入,暂时未用到
		addModule: function(){
			var ctrl = this;
			var form = this.get("form");

			ctrl.set("saveResult", null);
			
			this.store.ajax("POST", "configs/modulel7", {
				confType:form.confType
			}).then(function(response) {
				if(response.status=="success"){
					//todo: 保存成功逻辑
					alertTip("添加成功！！");
				}else{
					alertTip(response.message);
				}
			});
			ctrl.send("closeDialog","1");
		},

		//添加Lua模块方法
		addLuaModule: function(){
			var ctrl = this;
			var form = this.get("form");

			ctrl.set("formTip", null);
			
			this.store.ajax("POST", "configs/modulel7/Lua", {
				moduleName : form.moduleName
			}).then(function(response) {
				if(response.status=="success"){
					//todo: 保存成功逻辑
					alertTip("添加Lua模块成功！！");
				}else{
					alertTip(response.message);
				}
			});
			ctrl.send("closeDialog","2");
		},
		
		//启用禁用模块方法
		enableCtr: function(moduleId,status){
				var ctrl = this;
				ctrl.set("formTip", null);
				var statusTemp;
				if(status){
					// if(!confirm("确定启用该模块吗？")){
					// 	return false;
					// }
					// statusTemp=0;
					confirmDialog("确定启用该模块吗？").then(function(){
						statusTemp=0;
						ctrl.store.ajax("PUT", "configs/modulel7/status", {
							id:moduleId,
							status:statusTemp
						}).then(function(response){
							//todo: 保存结果返回逻辑
							if(response.status=="success"){
								ctrl.send("queryModules");
								if(statusTemp==1){
									alertTip("禁用模块成功！");
								}
								if(statusTemp==0){
									alertTip("启用模块成功！");
								}
							}else{
								alertTip(response.message);
							}
						});
					});
				}else{
					// if(!confirm("确定禁用该模块吗？")){
					// 	return false;
					// }
					// statusTemp=1;
					confirmDialog("确定禁用该模块吗？").then(function(){
						statusTemp=1;
						ctrl.store.ajax("PUT", "configs/modulel7/status", {
							id:moduleId,
							status:statusTemp
						}).then(function(response){
							//todo: 保存结果返回逻辑
							if(response.status=="success"){
								ctrl.send("queryModules");
								if(statusTemp==1){
									alertTip("禁用模块成功！");
								}
								if(statusTemp==0){
									alertTip("启用模块成功！");
								}
							}else{
								alertTip(response.message);
							}
						});
					});
				}
			},

		//导出模块方法
		leadOut: function(obj){
			var ctrl = this;
			confirmDialog("确定要导出该模块吗？")
			.then(function(){
				ctrl.set("formTip", null);
				var url="/front/rest/apm/configs/downloadL7Module?moduleName="+obj.name+"&l7_proto="+obj.l7_proto+"&token="+encodeURIComponent($.cookie("token"));;
				window.location.href =url;
			});
		},
		
		//删除模块方法
		deleteModule: function(moduleId,status){
			var ctrl = this;
			confirmDialog("确定要删除该模块吗？")
			.then(function(){
				var form = ctrl.get("form");
				ctrl.set("formTip", null);
				if(status == 0){
					alertTip("模块运行当中,请先禁用");
					return;
				}
				ctrl.store.ajax("DELETE", "configs/modulel7", {
					moduleId : moduleId
				}).then(function(response) {
					if(response.status=="success"){
						//todo: 保存成功逻辑
						alertTip("删除成功!");
						ctrl.send("queryModules");
					}else{
						alertTip(response.message);
					}
				});
			});
		},

		//添加模块返回码
		addModeCode:function(){
			var ctrl = this;

			if( !this.checkFormValid() ){
				return;
			}

			var form = this.get("form");
			ctrl.set("formTip", null);
			ctrl.set("saveResult", null);

			this.store.ajax("POST", "configs/modulel7/addRecode", {
				protocol : form.l7_proto,
				code:form.code,
				status:form.mstatus,
				errorMessage:form.errorMessage
			}).then(function(response) {
				if(response.status=="success"){
					//todo: 保存成功逻辑
					alertTip("添加返回码成功！");
					ctrl.set("form.code",null);
					ctrl.set("form.mstatus",null);
		 	 		ctrl.set("form.errorMessage",null);
		 	 		ctrl.send("closeDialog","5");
				}else{
					alertTip("添加失败！");
				}
					ctrl.send("queryModeCode");
			});
		
		},

		//修改模块返回码
		updaddModCode: function(obj){
			var ctrl = this;
			var form = this.get("form");

			ctrl.set("saveResult", null);
			ctrl.store.ajax("PUT", "configs/modulel7/updRecode", {
				protocol:form.protocol,
				id:form.id,
				code:form.code,
				status:form.mstatus,
				errorMessage:form.errorMessage
			}).then(function(response) {
				if(response.status=="success"){
					//todo: 保存成功逻辑
					alertTip("修改返回码成功！");
					ctrl.send("queryModeCode");
				}else{
					alertTip(response.message);
					ctrl.send("queryModeCode");
				}
			});
			ctrl.set("form.code","");
			ctrl.set("form.errorMessage","");
			ctrl.set("form.mstatus",null);
			ctrl.send("closeDialog","4");
		},

		//查询模块返回码列表
		queryModeCode:function(){
			var ctrl = this;
			var form = this.get("form");

			ctrl.set("saveResult", null);
			
			this.store.ajax("GET", "configs/modulel7/recodes", {
				l7_proto : form.l7_proto
			}).then(function(response) {
				if(response.status=="success"){
					//todo: 保存成功逻辑
					if(response.codeArr){
							var forEach = Ember.EnumerableUtils.forEach;
							var codeArrays=Ember.A(response.codeArr);
							forEach(codeArrays, function(errorCode){
									var s=errorCode.status;
									if(s==0)
										errorCode['statusName']='正确';
									if(s==1)
										errorCode['statusName']='错误';
									if(s==2)
										errorCode['statusName']='忽略';
							});
						ctrl.set("codeArrys", codeArrays);
					}
				}
			});
		},

		//删除模块返回码
		delModeCode:function(obj){
			var ctrl = this;
			confirmDialog("确定要删除该返回码吗？")
			.then(function(){
				var form = ctrl.get("form");
					ctrl.set("saveResult", null);
				var protocol=parseInt(obj.protocol);
				ctrl.store.ajax("DELETE", "configs/modulel7/delRecode", {
					protocol:protocol,
					id:obj.id,
					code:obj.code,
					errorMessage:obj.errorMessage
				}).then(function(response) {
					if(response.status=="success"){
						//todo: 保存成功逻辑
						alertTip("删除成功！");
						ctrl.send("queryModeCode");
					}else{
						alertTip("删除失败！");
					}
				});
			});
		},
		
		//上传C模块
		uploadModule: function(){
			var ctrl = this;
			ctrl.set("formTip", null);
			
			APM.iframeCallback = function(status,message) {
				if(status){
					if(status==1){
						alertTip('mod文件为空，请重新上传！');
					}
					if(status==2){
						alertTip('模块添加失败,文件应为mod格式！');
					}
					if(status==3){
						alertTip('模块运行当中,请先禁用.');
					}
					if(status==4){
						alertTip('模块已存在且主版本一致，添加失败.');
					}
					if(status==5){
						alertTip('模块添加成功！');
						ctrl.send("queryModules");
					}
					if(status==6){
						alertTip(message);
					}
				}
				APM.iframeCallback = null;
			};
			
			var form = $('#ssoForm')[0];
			form.method="POST";
			form.action="/front/rest/apm/configs/upload?token="+encodeURIComponent($.cookie("token"));
			form.submit();
			ctrl.send("closeDialog","1");
		}
	}
	
});